home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Financial
/
Stopwatch2.3
/
Source
/
createPath.c
< prev
next >
Wrap
Text File
|
1995-06-12
|
1KB
|
59 lines
#import <errno.h>
#import <string.h>
#import <stdio.h>
#import <libc.h>
#import <sys/types.h>
#import <sys/stat.h>
#import <objc/hashtable.h> /* for NXCopyStringBuffer */
#import "createPath.h"
/*
* Check each path element by iterating over the string, substituting
* a null byte for each slash and checking that the intermediate
* directory exists or can be created. The original string is uneffected.
*/
CreatePathErr
createPath( const char *callersPath, int mode )
{
char *slash, *ptr, *path;
CreatePathErr err = PathCreationOk;
ptr = path = NXCopyStringBuffer(callersPath);
/*
* No need to check root, plus it makes for
* an ugly boundary condition!
*/
if ( *ptr == '/' )
++ptr;
do {
struct stat statbuf;
if ( (slash = strchr( ptr, '/' )) != NULL ) {
*slash = '\0'; /* truncate it here for testing */
ptr = slash + 1; /* advance ptr for next iteration */
}
if ( stat( path, &statbuf ) < 0 ) {
/* if the dir doesn't exist, try to create it */
if ( errno == ENOENT ) {
if ( mkdir( path, mode ) < 0 )
err = PathMkdirFailure;
} else
err = PathStatFailure;
} else /* stat(2) succeeded */
if ( ! (statbuf.st_mode & S_IFDIR) )
err = PathNotDirectory;
if ( slash )
*slash = '/'; /* restore the slash */
} while ( err == PathCreationOk && slash != NULL );
free(path);
return err;
}